package org.gaaidou.ptarmigan.springboot.functional.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.gaaidou.ptarmigan.springboot.core.async.AsyncCaller;
import org.gaaidou.ptarmigan.springboot.core.async.ContextData;
import org.gaaidou.ptarmigan.springboot.functional.service.AsyncService;
import org.gaaidou.ptarmigan.springboot.functional.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    @Autowired
    private AsyncCaller asyncCaller;

    @Override
    public void log(UserVo userVo) {
        log.info("Server uuid: {}, thread id: {}", ContextData.getTxId(), Thread.currentThread().getName());

        asyncCaller.create().thenRunAsync(() -> {
            log.info("Async caller, uuid: {}, thread id: {}", ContextData.getTxId(), Thread.currentThread().getName());
        });

        asyncCaller.create(userVo).thenAcceptAsync(v -> {
            log.info("Async caller, user name is: {}, uuid: {}, thread id: {}", ContextData.getTxId(), v.getName(), Thread.currentThread().getName());
        });

        asyncCaller.exec(() -> {
            log.info("Spring task, uuid: {}, thread id: {}", ContextData.getTxId(), Thread.currentThread().getName());
        });
    }
}
